<?php

namespace app\conversations\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class ReamazeTagLogic extends BasicLogic
{
    
    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $date     = $params['createtime'] ?? '';

        $limit = $params['limit'] ?? 10;
        $where = [];

        if($date){
            $where[]  = ['created_at', '>=',$date[0]];
            $where[]  = ['created_at', '<=',$date[1] . 'T23:59:59.999Z'];
        }

        $list = Db::name('conversations')
            ->where($where)
            ->order('created_at desc')
            ->paginate($limit);

        $lists = $list->items();

        foreach ($lists as &$v) {
            switch ($v['status']) {
                case '0':
                    $v['status'] = '未解决';
                    break;
                case '1':
                    $v['status'] = '挂起';
                    break;
                case '2':
                    $v['status'] = '已解决';
                    break;
                case '3':
                    $v['status'] = '垃圾邮件';
                    break;
                case '4':
                    $v['status'] = '存档';
                    break;
                case '5':
                    $v['status'] = '暂停';
                    break;
                case '6':
                    $v['status'] = '自动解决';
                    break;
                case '7':
                    $v['status'] = '聊天机器人删除';
                    break;
                case '8':
                    $v['status'] = '聊天机器人解决';
                    break;
                case '9':
                    $v['status'] = '垃圾邮件-由AI识别';
                    break;
                default:
                    $v['status'] = '未知';
                    break;
            }
        }
        
        $data = [
            'list'  => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }
    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;
        Db::startTrans();
        try {
            //广告账户
            // $result = Db::name('advaccount')->insert($AdvertisingAccount);
            if ($result !== false) { 
                Db::commit();
                return json(DefaultResponse(200, true, '添加成功!', null));
            }
            Db::rollback();
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, '广告账户已存在!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));

    }

    public function find($id)
    {
        $where['id'] = $id;
        $row = Db::name('conversations')
            ->where($where)
            ->find();
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        $row = $this->model->find($id);
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;
        Db::startTrans();
        try {
            // $result = $row->save($AdvertisingAccount);
            if ($result) {
                Db::commit();
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            Db::rollback();
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        Db::rollback();
        return json(DefaultResponse(200, false, '更新成功!', null));
    }

    /**
    * @desc    实时记录今天业绩
    * @date    2024/7/16
    * @author  zhangheng
    */
    public function export($params = [])
    {
        $header = [];
        $header[] = ['会话标识', 'slug'];
        $header[] = ['客户', 'author'];
        $header[] = ['时间', 'created_at'];
        $header[] = ['状态', 'status'];
        $tableName = $this->model->getName();

        $date     = $params['createtime'] ?? '';

        $where = [];

        if($date){
            $where[]  = ['created_at', '>=',$date[0]];
            $where[]  = ['created_at', '<=',$date[1] . 'T23:59:59.999Z'];
        }
        $lists = Db::name('conversations')
            ->where($where)
            ->order('created_at desc')
            ->select()->toarray();

        foreach ($lists as &$v) {
            switch ($v['status']) {
                case '0':
                    $v['status'] = '未解决';
                    break;
                case '1':
                    $v['status'] = '挂起';
                    break;
                case '2':
                    $v['status'] = '已解决';
                    break;
                case '3':
                    $v['status'] = '垃圾邮件';
                    break;
                case '4':
                    $v['status'] = '存档';
                    break;
                case '5':
                    $v['status'] = '暂停';
                    break;
                case '6':
                    $v['status'] = '自动解决';
                    break;
                case '7':
                    $v['status'] = '聊天机器人删除';
                    break;
                case '8':
                    $v['status'] = '聊天机器人解决';
                    break;
                case '9':
                    $v['status'] = '垃圾邮件-由AI识别';
                    break;
                default:
                    $v['status'] = '未知';
                    break;
            }
        }
        $fileName = "export_" . $tableName . "_" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

}